Ext.onReady(function() {

	Ext.BLANK_IMAGE_URL = 'ext-3.2.1/resources/images/access/tree/s.gif';
	// 用户默认文件夹
		var basefolder = "F:\\workspace\\webDemo\\WebRoot\\userfile\\js";
		var rootnode = new Ext.tree.AsyncTreeNode( {
			id : 'rootNode',
			text : 'root',
			draggable : false,
			id : basefolder
		});
		var Module = (function(my) {
			my.north = new Ext.BoxComponent( {
				id : 'north-panel',
				region : 'north',
				el : 'north',
				height : 32
			});
			this.hendlerManager = [];

			return my;
		}(Module || {}));

		var Module = (function(my) {
			my.south = {
				id : 'south-panel',
				region : 'south',
				contentEl : 'south',
				split : true,
				height : 100,
				minSize : 100,
				maxSize : 200,
				collapsible : true,
				title : '控制台',
				margins : '0 0 0 0'
			};
			return my;
		}(Module || {}));

		var Module = (function(my) {
			my.east = {
				id : 'east-panel',
				region : 'east',
				title : '',
				collapsible : true,
				split : true,
				width : 225,
				minSize : 175,
				maxSize : 400,
				layout : 'fit',
				margins : '0 5 0 0',
				items : new Ext.TabPanel( {
					border : false,
					activeTab : 1,
					tabPosition : 'bottom',
					items : [ {
						html : '<p>A TabPanel component can be a region.</p>',
						title : 'A Tab',
						autoScroll : true
					}, new Ext.grid.PropertyGrid( {
						id : 'propGrid',
						title : '成员属性列表',
						closable : true,
						source : {
							"(name)" : "Properties Grid",
							"grouping" : false,
							"autoFitColumns" : true,
							"productionQuality" : false,
							"created" : new Date(Date.parse('10/15/2006')),
							"tested" : false,
							"version" : .01,
							"borderWidth" : 1
						}
					})]
				})
			};
			return my;
		}(Module || {}));

		var Module = (function(my) {
			my.addTab = function(node) {
				var title = "newFile" + tabindex;
				var filename = "";
				// 打开的tab是否是新建的tab 或是从资源管理器中打开
				var isnew = true;
				if (node instanceof Ext.data.Node) {
					title = node.text;
					filename = node.text;
					isnew = false;
				}
				var tabs = Ext.getCmp("center-region");
				// text = response.responseText;
				tabs.add( {
					// id : "editor_tab_" + tabindex,
						title : title,
						index : tabindex,
						xtype : 'panel',
						isnew : isnew,
						tbar : {
							width : 30,
							items : [ {
								icon : 'pic/refresh.gif',
								cls : 'x-btn-icon',
								tooltip : '新建',
								text : '新建',
								handler : my.addTab
							}, {
								icon : 'pic/redo.gif',
								cls : 'x-btn-icon',
								text : '重做',
								tooltip : '重做'
							}, {
								icon : 'pic/undo.gif',
								cls : 'x-btn-icon',
								text : '返回',
								tooltip : '返回'
							}, {
								icon : 'pic/save.gif',
								text : '保存',
								tooltip : '保存'
							}, {
								icon : 'images/execute.bmp',
								cls : 'x-btn-icon',
								text : '执行',
								tooltip : '执行',
								handler : execute
							}, {
								text : '插入注释',
								tooltip : '插入注释',
								handler : doAnnotation
							}]
						},
						html : "",
						closable : true,
						listeners : {
							"render" : function() {
								// alert(node.id.replaceall('\\', '\\\\'));
								if (filename == "")
									filename = title + ".js";
								t.overwrite(this.body, {
									src : 'codepresstest.jsp',
									filename : filename,
									basefolder : basefolder,
									id : Ext.id(this)
								});
							},
							"close" : function(tab) {
								// 关闭一个新建tab 该tab后面的tab序号都减1
								if (tab.isnew) {
									// alert(tab.find("index",1));
									return;
									var ids = tab.id.split("-");
									var index = (new Number(ids[ids.length - 1]))
											+ 7;
									ids.splice(ids.length - 1, 1, "");
									var pretabid = ids.join("-");
									while ((temp = Ext.getCmp(pretabid + index))) {
										// temp.reName("newFile" + (temp.index -
										// 1),(temp.index - 1));
										temp.setTitle("newFile"
												+ (temp.index - 1));
										temp.setIndex((temp.index - 1));
										index = index + 7;
									}
									tabindex--;
									Ext.log('close tab' + tabindex)
								}
							}
						}
					}).show();
				if (isnew)
					tabindex++;
			};
			my.west = {
				region : 'west',
				id : 'west-panel',
				title : '',
				split : true,
				width : 200,
				minSize : 175,
				maxSize : 400,
				collapsible : true,
				margins : '0 0 0 5',
				layout : 'accordion',
				layoutConfig : {
					animate : true
				},
				items : [new Ext.tree.TreePanel( {
					id : 'west-nav',
					contentEl : 'west',
					iconCls : 'settings',
					title : '资源管理器',
					animate : true,
					autoScroll : true,
					clearOnLoad : true,
					tbar : {
						width : 30,
						items : [ {
							icon : 'images/add.gif',
							tooltip : '新建'
						}, {
							icon : 'images/del.gif',
							tooltip : '删除',
							handler : delNode
						}, {
							icon : 'images/upload.png',
							cls : 'x-btn-icon',
							tooltip : '上传'
						}, {
							text : '刷新',
							cls : 'x-btn-icon',
							tooltip : '刷新',
							handler : refurbish
						}, {
							text : '重命名',
							cls : 'x-btn-icon',
							tooltip : '重命名',
							handler : reName
						}]
					},
					loader : new dLoader( {
						id : 'treeLoader',
						dataUrl : 'extAjax',
						listeners : {
							beforeload : function(treeLoader, node) {
								this.baseParams.path = node.id;
							}
						},
						baseParams : {
							mothed : "treeloader"
						}
					}),
					root : rootnode,
					enableDD : true,
					containerScroll : true,
					listeners : {
						dblclick : my.addTab
					},
					border : false
				}), {
					id : 'west-settings',
					title : '设置',
					html : '<p>Some settings in here.</p>',
					border : false,
					iconCls : 'settings'
				}]
			};

			// 插入注释
			function doAnnotation() {
				var tabs = Ext.getCmp("center-region");
				var p = tabs.getActiveTab();
			};
			// 删除节点
			function delNode() {
				var item = Ext.getCmp("west-nav").getSelectionModel()
						.getSelectedNode();

				if (item == null) {
					log("请先选中一个文件");
					return;
				}
				Ext.Ajax.request( {
					params : {
						filePath : basefolder + "/" + item.text,
						mothed : "delFile"
					},
					url : "extAjax",
					success : function(response) {
						if (response.responseText == "succ") {
							refurbish();
						} else {
							Ext.Msg.alert("重命名操作失败");
						}
					}
				});
			};
			// 刷新树
			function refurbish() {
				var l = Ext.getCmp("west-nav").getLoader();
				l.load(rootnode, function() {
					Ext.getCmp("west-nav").expandAll();
				});
			};
			// 重命名
			function reName() {
				var item = Ext.getCmp("west-nav").getSelectionModel()
						.getSelectedNode();

				if (item == null) {
					log("请先选中一个文件");
					return;
				}

				Ext.Msg.prompt('重命名对话框', '请输入新的文件名称:', function(btn, text) {
					if (btn == 'ok') {
						if (!text.match(/.js/g)) {
							text = text + ".js";
						}
						// 调用父窗口 把代码传到父窗口
						// var tabs = parent.Ext.getCmp("center-region");
						// var p = tabs.getActiveTab();
						// p.setTitle(text);
						// filename=text;
						var oldpath = basefolder + "/" + item.text;
						var newpath = basefolder + "/" + text;
						Ext.Ajax.request( {
							params : {
								oldPath : oldpath,
								newPath : newpath,
								mothed : "renameFile"
							},
							url : "extAjax",
							success : function(response) {
								if (response.responseText == "succ") {
									refurbish();
								} else {
									Ext.Msg.alert("重命名操作失败");
								}
							}
						});
					}
				});
			};
			return my;
		}(Module || {}));

		var Module = (function(my) {
			my.center = new Ext.TabPanel( {
				id : 'center-region',
				region : 'center',
				autoScroll : true,
				enableTabScroll : true,
				deferredRender : false,
				activeTab : 0,
				items : [{
					id : 'defaultEditorID',
					contentEl : 'center2',
					title : '默认的首页',
					xtype : 'panel',
					labelAlign : "left",
					layout : "table",
					layoutConfig : {
						columns : 2
					},
					tbar : {
						width : 30,
						items : [ {
							icon : 'images/refresh.gif',
							cls : 'x-btn-icon',
							text : '新建',
							tooltip : '新建',
							handler : my.addTab
						}, {
							icon : 'images/redo.gif',
							cls : 'x-btn-icon',
							text : '重做',
							tooltip : '重做'
						}, {
							icon : 'images/undo.gif',
							cls : 'x-btn-icon',
							text : '返回',
							tooltip : '返回'
						}, {
							icon : 'images/save.gif',
							text : '保存',
							tooltip : '保存'
						}, {
							icon : 'images/execute.bmp',
							cls : 'x-btn-icon',
							text : '执行',
							tooltip : '执行',
							handler : execute
						}, {
							text : '插入注释',
							tooltip : '插入注释',
							handler : doAnnotation
						}]
					},
					items : [ {
						xtype : "panel",
						border : false,
						id : 'ext-radiogroup-panel',
						colspan : 2,
						layoutConfig : {
							deferredRender : false
						},
						layout : "column",
						width : 400,
						style : 'fontSize:16;margin:100 0 5 160', // 上右下左

							items : [{
								xtype : 'radiogroup',
								items : [ {
									boxLabel : 'JavaScript',
									name : 'rb-auto',
									inputValue : 1
								}, {
									boxLabel : 'ExtJS',
									name : 'rb-auto',
									inputValue : 2,
									checked : true
								}, {
									boxLabel : 'Jquery',
									name : 'rb-auto',
									inputValue : 3
								}, {
									boxLabel : 'YUI',
									name : 'rb-auto',
									inputValue : 4
								}, {
									boxLabel : 'RegExp',
									name : 'rb-auto',
									inputValue : 5
								}]
							}]
						}, {
							xtype : "panel",
							id : 'ext-search-text-panel',
							layout : "column",
							width : 700,
							border : false,
							colspan : 2,
							items : [ {
								xtype : "textfield",
								id : 'ext-search-text',
								height : 25,
								style : 'margin:0 5 0 150;font-size: 16px',
								width : 400
							}, {
								xtype : "button",
								height : 25,
								style : 'font-size: 16px',
								handler : doSearch,
								text : "搜索一下"
							}]
						}]
				}]
			});
			// 搜索
			function doSearch() {
				// 获得输入框的内容
				var searchText = Ext.getCmp('ext-search-text').getValue();
				if (searchText == '') {
					log('请输入关键字');
					return;
				}
				var d = ['<li><a href=# >aaaaa</a></li>', '<li>aaaaa</li>',
						'<li>aaaaa</li>', '<li>aaaaa</li>', '<li>aaaaa</li>'];

				var ds = new Ext.data.Store( {
					proxy : new Ext.data.ScriptTagProxy( {
						url : 'http://extjs.com/forum/topics-remote.php'
					}),
					reader : new Ext.data.JsonReader( {
						root : 'topics',
						totalProperty : 'totalCount',
						id : 'post_id'
					}, [ {
						name : 'postId',
						mapping : 'post_id'
					}, {
						name : 'title',
						mapping : 'topic_title'
					}, {
						name : 'topicId',
						mapping : 'topic_id'
					}, {
						name : 'author',
						mapping : 'author'
					}, {
						name : 'lastPost',
						mapping : 'post_time',
						type : 'date',
						dateFormat : 'timestamp'
					}, {
						name : 'excerpt',
						mapping : 'post_text'
					}]),

					baseParams : {
						limit : 20,
						forumId : 4
					}
				});
				var store = new Ext.data.Store( {
					url : 'extAjax',
					reader : new Ext.data.JsonReader( {
						root : 'topics',
						totalProperty : 'totalCount',
						id : 'post_id'
					}, [ {
						name : 'postId',
						mapping : 'post_id'
					}, {
						name : 'title',
						mapping : 'topic_title'
					}, {
						name : 'topicId',
						mapping : 'topic_id'
					}, {
						name : 'author',
						mapping : 'author'
					}, {
						name : 'lastPost',
						mapping : 'post_time',
						type : 'date',
						dateFormat : 'timestamp'
					}, {
						name : 'excerpt',
						mapping : 'post_text'
					}]),

					baseParams : {
						limit : 20,
						saerchText : '*' + searchText + '*',
						mothed : "doSearch",
						forumId : 4
					}
				});

				// Custom rendering Template for the View
				var resultTpl = new Ext.XTemplate(
						'<tpl for=".">',
						'<div class="search-item">',
						'<h3><span>{lastPost:date("M j, Y")}<br />by {author}</span>',
						'<a href="http://extjs.com/forum/showthread.php?t={topicId}&p={postId}" target="_blank">{title}</a></h3>',
						'<p>{excerpt}</p>', '</div></tpl>');

				var panel = new Ext.Panel( {
					height : 500,
					autoScroll : true,
					border : false,
					style : 'margin:10 0 0 0',
					items : new Ext.DataView( {
						tpl : resultTpl,
						store : ds,
						itemSelector : 'div.search-item'
					}),

					bbar : new Ext.PagingToolbar( {
						store : ds,
						pageSize : 20,
						displayInfo : true,
						displayMsg : 'Topics {0} - {1} of {2}',
						emptyMsg : "No topics to display"
					})
				});

				ds.load( {
					params : {
						start : 0,
						limit : 20,
						forumId : 4
					}
				});

				Ext.Ajax
						.request( {
							params : {
								saerchText : '*' + searchText + '*',
								mothed : "doSearch"
							},
							url : "index.do",
							success : function(response) {
								log(response.responseText);
								if (response.statusText == 'OK') {
									Ext.getDom('ext-radiogroup-panel').style.marginLeft = "10px";
									Ext.getDom('ext-radiogroup-panel').style.marginTop = "30px"
									Ext.getDom('ext-search-text').style.marginLeft = "10px";

									var _panel = Ext.getCmp('defaultEditorID');
									_panel.add(panel);
									_panel.doLayout();
								}
								// log(response.responseText);

							},
							onFailure : function(response) {
								logforObject(response);
							}
						});
			};

			// 执行代码
			function execute() {
				var tabs = Ext.getCmp("center-region");
				var p = tabs.getActiveTab();
				var tab_code = p.getCode();
				tab_code = tab_code.replace(/&lt;/g, '<');
				tab_code = tab_code.replace(/&gt;/g, '>');
				// log(tab_code);
				// alert(tab_code);
				// var tab_code="for(var temp in d ) {
				// Ext.log(temp+':'+d[temp])} ;
				eval(tab_code);
				// d.show();
			};
			// 插入注释
			function doAnnotation() {
				var tabs = Ext.getCmp("center-region");
				var p = tabs.getActiveTab();
			};
			return my;
		}(Module || {}));

		var viewport = new Ext.Viewport( {
			id : 'viewport-component',
			layout : 'border',
			items : [Module.north, Module.south, Module.east, Module.west,
					Module.center]
		});

	});

var dLoader = Ext.extend(Ext.tree.TreeLoader, {
	processResponse : function(response, node, callback, scope) {
		var json = response.responseText;
		// alert(json);
		try {
			var o = response.responseData || Ext.decode(json);
			node.beginUpdate();
			for (var i = 0, len = o.length;i < len; i++) {
				var n = this.createNode(o[i]);
				if (n) {
					node.appendChild(n);
				}
			}
			node.endUpdate();
			this.runCallback(callback, scope || node, [node]);
		} catch (e) {
			this.handleFailure(response);
		}
	}
});